From cfeb1a1ee25865b2d8ba7b8a5ea4ad815077b353 Mon Sep 17 00:00:00 2001
From: Arsen Chaloyan <achaloyan@gmail.com>
Date: Wed, 23 Apr 2014 13:33:03 -0700
Subject: [PATCH] Comply with the aggressive-loop-optimization introduced in
 gcc 4.8.

The problem is in gcc loop optimizers which detect a loop operating with out of bounds memory and then optimize it with something undefined since, according to the C standard, out of bounds behavior is undefined. The problem could have been addressed by using the new option -fno-aggressive-loop-optimization as well.
---
 libsofia-sip-ua/msg/msg_parser.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/libsofia-sip-ua/msg/msg_parser.c b/libsofia-sip-ua/msg/msg_parser.c
index 5a1ef09..0c88b1f 100644
--- a/libsofia-sip-ua/msg/msg_parser.c
+++ b/libsofia-sip-ua/msg/msg_parser.c
@@ -2468,8 +2468,6 @@ int msg_header_prepend(msg_t *msg,
 msg_header_t **
 msg_hclass_offset(msg_mclass_t const *mc, msg_pub_t const *mo, msg_hclass_t *hc)
 {
-  int i;
-
   assert(mc && hc);
 
   if (mc == NULL || hc == NULL)
@@ -2481,12 +2479,16 @@ msg_hclass_offset(msg_mclass_t const *mc, msg_pub_t const *mo, msg_hclass_t *hc)
       if (mc->mc_hash[j].hr_class == hc) {
 	return (msg_header_t **)((char *)mo + mc->mc_hash[j].hr_offset);
       }
-  }
-  else
+  } else {
     /* Header has no name. */
-    for (i = 0; i <= 6; i++)
-      if (hc->hc_hash == mc->mc_request[i].hr_class->hc_hash)
-	return (msg_header_t **)((char *)mo + mc->mc_request[i].hr_offset);
+    if (hc->hc_hash == mc->mc_request[0].hr_class->hc_hash) return (msg_header_t **)((char *)mo + mc->mc_request[0].hr_offset);
+    if (hc->hc_hash == mc->mc_status[0].hr_class->hc_hash) return (msg_header_t **)((char *)mo + mc->mc_status[0].hr_offset);
+    if (hc->hc_hash == mc->mc_separator[0].hr_class->hc_hash) return (msg_header_t **)((char *)mo + mc->mc_separator[0].hr_offset);
+    if (hc->hc_hash == mc->mc_payload[0].hr_class->hc_hash) return (msg_header_t **)((char *)mo + mc->mc_payload[0].hr_offset);
+    if (hc->hc_hash == mc->mc_unknown[0].hr_class->hc_hash) return (msg_header_t **)((char *)mo + mc->mc_unknown[0].hr_offset);
+    if (hc->hc_hash == mc->mc_error[0].hr_class->hc_hash) return (msg_header_t **)((char *)mo + mc->mc_error[0].hr_offset);
+    if (hc->hc_hash == mc->mc_multipart[0].hr_class->hc_hash) return (msg_header_t **)((char *)mo + mc->mc_multipart[0].hr_offset);
+  }
 
   return NULL;
 }
-- 
1.8.0.msysgit.0

